<?php
require_once ("../db/connectdatabase.php");
date_default_timezone_set("Asia/Saigon");

function updateProfile($id, $name, $phone, $status, $inforequest, $amount1, $amount2, $amount3) {
	$con = new ConnectDB();
	
	$tbl = "profilecustomer";
	$fn = array('namecustomer' => $name, 'phonenumber' => $phone, 'statusquo' => $status, 'inforequest' => $inforequest, 'amount1' => $amount1, 'amount2' => $amount2, 'amount3' => $amount3, 'dateupdate' => time());
	$condition = array('idpro' => $id);
	return $con -> update($tbl, $fn, $condition);
}

function updateProfileAbort($id, $name, $phone, $status, $inforequest) {
	$con = new ConnectDB();
	
	$tbl = "profilecustomer";
	$fn = array('namecustomer' => $name, 'phonenumber' => $phone, 'statusquo' => $status, 'inforequest' => $inforequest, 'isgnore' => true, 'dateupdate' => time());
	$condition = array('idpro' => $id);
	return $con -> update($tbl, $fn, $condition);
}


function deleteProfile($id) {
	$con = new ConnectDB();
	$tbl = "profilecustomer";
	$condition = array('idpro' => $id);
	return $con -> delete($tbl, $condition);
}

$action = $_POST['act'];
if($action == 'edit') {
	$result = false;
	if(isset($_POST['rei']) && isset($_POST['name']) && isset($_POST['phone']) && isset($_POST['stre']) && isset($_POST['cont']) && isset($_POST['amo1']) && isset($_POST['amo2']) && isset($_POST['amo3'])) {
		$id = $_POST['rei'];
		$name = $_POST['name'];
		$phone = $_POST['phone'];
		$status = $_POST['stre'];
		$inforequest = $_POST['cont'];
		
		$currentDate = date("d-m-Y H:i");
		$inforequest .= '<p>Ng&agrave;y Kiểm Duyệt: '.$currentDate.'</p><hr /><p>&nbsp;</p>';
		if(isset($_POST['ifretemp'])) {
			$inforeqtemp = $_POST['ifretemp'];
			$inforequest = $inforeqtemp.$inforequest;
		}
		
		if($status == "Từ chối") {
			$result = updateProfileAbort($id, $name, $phone, $status, $inforequest);
		} else {
			$amount1 = $_POST['amo1'];
			$amount2 = $_POST['amo2'];
			$amount3 = $_POST['amo3'];
			$result = updateProfile($id, $name, $phone, $status, $inforequest, $amount1, $amount2, $amount3);
		}
		
		
	}
	echo json_encode($result);
} else if($action == 'del') {
	if(isset($_POST['idpe'])) {
		echo json_encode(deleteProfile($_POST['idpe']));
	}
}
?>